//
//  MJMyMsgController.m
//  WeCahtQQLogin
//
//  Created by HappyMilk 02 on 2017/2/8.
//  Copyright © 2017年 HappyMilk 02. All rights reserved.
//

#import "MJMyMsgController.h"
#import "MJMsgCell.h"
#import "MJMsgHeadCell.h"
#import "MJChangeController.h"

#import <AVOSCloud/AVOSCloud.h>
#import "SVProgressHUD.h"
#import "MJAdressController.h"
#import "MJCityController.h"

#import "MJHeightPiker.h"
#import "MJBirthdayPiker.h"

#define HEXCOLOR(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface MJMyMsgController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tabelview;


@property (nonatomic ,strong) UIActionSheet *sexSheet;
@property (nonatomic ,strong) UIActionSheet *schoolSheet;

@property (nonatomic ,assign) NSInteger adrsIndex;

@end

@implementation MJMyMsgController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeAddress:) name:@"address" object:nil];
    
    [self setExtraCellLineHidden:self.tabelview];
}
-(void)setExtraCellLineHidden: (UITableView *)tableView{
    UIView *view = [UIView new];
    view.backgroundColor = HEXCOLOR(0xf5f5f5);
    [tableView setTableFooterView:view];
}

- (void)changeAddress:(NSNotification *)noti{
    NSString *adr = [noti.userInfo objectForKey:@"address"];
    if (_adrsIndex == 1) {
        _userModel.adrs = adr;
        [_userModel saveInBackground];
        [self.tabelview reloadData];
    }else{
        _userModel.adrs_now = adr;
        [_userModel saveInBackground];
        [self.tabelview reloadData];
    }
    
}
- (IBAction)popvc:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 3;
            break;
        case 1:
            return 6;
            break;
        default:
            return 2;
            break;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        static NSString *cellid = @"MJMsgHeadCell";
        MJMsgHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MJMsgHeadCell" owner:nil options:nil] lastObject];
        }
        cell.headUrl = self.userModel.head_img.url;
        return cell;
    }
    static NSString *cellid = @"MJMsgCell";
    MJMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MJMsgCell" owner:nil options:nil] lastObject];
    }
    [cell createCellByIndexPath:indexPath andUser:self.userModel];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 70;
        }
        return 40;
    }
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 14;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = HEXCOLOR(0xf5f5f5);
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MJMsgCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
            UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"从相册上传", nil];
            [sheet showInView:self.view];
        }else if (indexPath.row == 1 | indexPath.row == 2){
            
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"mine" bundle:nil];
            MJChangeController *vc = [sb instantiateViewControllerWithIdentifier:@"MJChangeController"];
            vc.change_title = cell.label_title.text;
            [self.navigationController pushViewController:vc animated:YES];
            vc.changeMsg = ^(NSString *text){
                if (indexPath.row == 1) {
                    _userModel.name = text;
                    [_userModel saveInBackground];
                    [self.tabelview reloadData];
                }else{
                    _userModel.phoneNum = text;
                    [_userModel saveInBackground];
                    [self.tabelview reloadData];
                }
            };
            
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            _sexSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"男",@"女", nil];
            [_sexSheet showInView:self.view];
        }else if (indexPath.row == 1 | indexPath.row == 2){
            _adrsIndex = indexPath.row;
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"mine" bundle:nil];
            MJAdressController *vc = [sb instantiateViewControllerWithIdentifier:@"MJAdressController"];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if (indexPath.row == 3){//生日
            MJBirthdayPiker *piker = [[MJBirthdayPiker alloc] initWithFrame:(CGRect){0,0,SCREEN_WIDTH-60,260}];
            piker.center = self.view.center;
            [piker showBirthdayPikerViewInView:self.view];
            piker.chioceBirthday = ^(NSString *text){
                _userModel.birthday = text;
                [_userModel saveInBackground];
                [self.tabelview reloadData];
            };
        }else if (indexPath.row == 4){//身高
            
            MJHeightPiker *piker = [[MJHeightPiker alloc] initWithFrame:(CGRect){0,0,SCREEN_WIDTH-60,260}];
            piker.center = self.view.center;
            [piker showHeightPikerViewInView:self.view];
            piker.chioceHeight = ^(NSString *text){
                NSString *str = [NSString stringWithFormat:@"%@ Cm",text];
                _userModel.height = str;
                [_userModel saveInBackground];
                [self.tabelview reloadData];
            };
        }else{
            _schoolSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"专科",@"本科",@"研究生",@"博士",@"其他", nil];
            [_schoolSheet showInView:self.view];
        }
    }else if (indexPath.section == 2){

        if (indexPath.row == 0 | indexPath.row == 1) {
            
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"mine" bundle:nil];
            MJChangeController *vc = [sb instantiateViewControllerWithIdentifier:@"MJChangeController"];
            vc.change_title = cell.label_title.text;
            [self.navigationController pushViewController:vc animated:YES];
            vc.changeMsg = ^(NSString *text){
                if (indexPath.row == 0) {
                    _userModel.enmotion = text;
                    [_userModel saveInBackground];
                    [self.tabelview reloadData];
                }else{
                    _userModel.signature = text;
                    [_userModel saveInBackground];
                    [self.tabelview reloadData];
                }
            };
            
        }else{
        
        }
    }
    
    
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 8_3) __TVOS_PROHIBITED{
    if(actionSheet == _sexSheet){
        
        if(buttonIndex == 0){
            self.userModel.sex = @"男";
            [_userModel saveInBackground];
            [self.tabelview reloadData];
        }else if(buttonIndex == 1){
            self.userModel.sex = @"女";
            [_userModel saveInBackground];
            [self.tabelview reloadData];
        }
//        if(buttonIndex == 0){
//            self.userModel.sex = @"男";
//            [_userModel saveInBackground];
//            [self.tabelview reloadData];
//        }else if(buttonIndex == 1){
//            self.userModel.sex = @"女";
//            [_userModel saveInBackground];
//            [self.tabelview reloadData];
//        }
        
    }else if(actionSheet == _schoolSheet){
        if(buttonIndex == 0){
            self.userModel.school_record = @"专科";
            [_userModel saveInBackground];
            [self.tabelview reloadData];
        }else if(buttonIndex == 1){
            self.userModel.school_record = @"本科";
            [_userModel saveInBackground];
            [self.tabelview reloadData];
        }else if(buttonIndex == 2){
            self.userModel.school_record = @"研究生";
            [_userModel saveInBackground];
            [self.tabelview reloadData];
        }else if(buttonIndex == 3){
            self.userModel.school_record = @"博士";
            [_userModel saveInBackground];
            [self.tabelview reloadData];
        }else if(buttonIndex == 4){
            self.userModel.school_record = @"其他";
            [_userModel saveInBackground];
            [self.tabelview reloadData];
        }
//        if(buttonIndex == 0){
//            self.userModel.school_record = @"专科";
//            [_userModel saveInBackground];
//            [self.tabelview reloadData];
//        }else if(buttonIndex == 1){
//            self.userModel.school_record = @"本科";
//            [_userModel saveInBackground];
//            [self.tabelview reloadData];
//        }else if(buttonIndex == 2){
//            self.userModel.school_record = @"研究生";
//            [_userModel saveInBackground];
//            [self.tabelview reloadData];
//        }else if(buttonIndex == 3){
//            self.userModel.school_record = @"博士";
//            [_userModel saveInBackground];
//            [self.tabelview reloadData];
//        }else if(buttonIndex == 4){
//            self.userModel.school_record = @"其他";
//            [_userModel saveInBackground];
//            [self.tabelview reloadData];
//        }
        
    }else{
        if (buttonIndex == 0) {
            //拍照
            UIImagePickerController *controller = [[UIImagePickerController alloc] init];
            controller.sourceType = UIImagePickerControllerSourceTypeCamera;
            controller.delegate = self;
            [self presentViewController:controller animated:YES completion:nil];
        } else if (buttonIndex == 1) {
            //从相册中选取
            UIImagePickerController *controller = [[UIImagePickerController alloc] init];
            controller.delegate = self;
            [self presentViewController:controller animated:YES completion:nil];
        }
    }
}
#pragma mark --- UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:^() {
        
        [SVProgressHUD show];
        
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        AVFile *file = [AVFile fileWithData:UIImagePNGRepresentation(image)];
        _userModel.head_img = file;
        [_userModel saveInBackground];
        
        [SVProgressHUD dismiss];
        [self.tabelview reloadData];
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
//// returns the number of 'columns' to display.
//- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
//    return 3;
//}
//
//// returns the # of rows in each component..
//- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
//    return 5;
//}



@end
